import Vue from "vue";
import VueRouter from "vue-router";
const originalPush = VueRouter.prototype.push;
const originalGo = VueRouter.prototype.go;
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function push(...args) {
  ;
  return Promise.resolve(originalPush.call(this, ...args));
};
VueRouter.prototype.go = function push(...args) {
  ;
  return Promise.resolve(originalGo.call(this, ...args));
};
VueRouter.prototype.replace = function push(...args) {
  ;
  return Promise.resolve(originalReplace.call(this, ...args));
};

Vue.use(VueRouter);

const routes = [
  {
    path: "",
    redirect: "/home",
  },

  {
    path: "/cart",
    name: "Cart",
    component: () => import("views/cart/Cart"),
    children: [
      {
        path: "confirm/:type",
        name: "ConfirmOrder",
        component: () => import("views/cart/pay/ConfirmOrder"),
        meta: {
          requiresAuth: true,
          keepAlive: true,
        },
      },
    ],
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/my",
    name: "My",
    component: () => import("views/my/My"),

    children: [
      {
        path: "viewed",
        name: "Viewed",
        component: () => import("views/my/childCpn/MyViewed"),
        meta: {
          requiresAuth: true,
          keepAlive: true,
        },
      },
      {
        path: "collect",
        name: "Collect",
        component: () => import("views/my/childCpn/MyCollect"),
        meta: {
          requiresAuth: true,
          keepAlive: true,
        },
      },
      {
        path: "order/:idx",
        name: "Order",
        component: () => import("views/my/order/MyOrder"),
        meta: {
          requiresAuth: true,
          keepAlive: true,
        },
      },
      {
        path: "order_detail",
        name: "OrderDetail",
        component: () => import("views/my/order/OrderDetail"),
        meta: {
          requiresAuth: true,
          keepAlive: false,
        },
      },
      {
        path: "setting",
        name: "Setting",
        component: () => import("views/my/setting/MySetting"),
        meta: {
          requiresAuth: true,
          keepAlive: false,
        },
      },
      {
        path: "address",
        name: "Address",
        component: () => import("views/my/address/Address"),
        meta: {
          requiresAuth: true,
          keepAlive: false,
        },
        children: [
          {
            path: "edit",
            name: "EditAddress",
            component: () => import("views/my/address/EditAddress"),
            meta: {
              requiresAuth: true,
              keepAlive: false,
            },
          },
          {
            path: "add",
            name: "AddAddress",
            component: () => import("views/my/address/AddAddress"),
            meta: {
              requiresAuth: true,
              keepAlive: false,
            },
          },
        ],
      },
    ],
    meta: {
      requiresAuth: true,
      keepAlive: true,
    },
  },
  {
    path: "/home",
    name: "Home",
    component: () => import("views/home/Home"),
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/category",
    name: "Category",
    component: () => import("views/category/Category"),
    meta: {
      keepAlive: true,
    },
  },
  {
    path: "/detail",
    name: "Detail",
    component: () => import("views/detail/Detail"),
    meta: {
      keepAlive: false,
    },
  },

  {
    path: "/login",
    name: "Login",
    component: () => import("views/user/Login"),
    meta: {
      keepAlive: false,
    },
  },
  {
    path: "/register",
    name: "Register",
    component: () => import("views/user/Register"),
    meta: {
      keepAlive: false,
    },
  },
  {
    path: "/search",
    name: "SearchPage",
    component: () => import("views/search/SearchPage"),
    meta: {
      keepAlive: true,
    },
    children: [
      {
        path: "detail",
        name: "SearchDetail",
        component: () => import("views/detail/Detail"),
        meta: {
          keepAlive: false,
        },
      },
    ],
  },
  {
    path: "/404",
    name: "NotFound",
    component: () => import("views/notFound"),
  },
  {
    path: "*",
    redirect: "Home",
  },
];

const router = new VueRouter({
  mode: "history",
  routes,
});

router.beforeEach((to, from, next) => {
  //判断是否需要登录
  let token = localStorage.token;
  if (token && to.path == "/login") {
    next("/my");
  } else if (to.meta.requiresAuth && !token) {
    next("/login");
  } else {
    next();
  }
});

export default router;
